package com.example.lwy.yuejian11;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.example.lwy.yuejian11.Base.BaseActivity;
import com.example.lwy.yuejian11.Base.Const;
import com.example.lwy.yuejian11.Services.RetrofitServices;
import com.example.lwy.yuejian11.bean.Login;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class LoginActivity extends BaseActivity {

    private String username,password;
    SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        sp = getSharedPreferences("user_info", Context.MODE_PRIVATE);

        final Button register_btn = (Button)findViewById(R.id.logregister_btn);
        Button login_btn = (Button)findViewById(R.id.loglogin_btn);
        final CheckBox remember_cb = (CheckBox)findViewById(R.id.remember_cb);
        final EditText username_ed = (EditText)findViewById(R.id.logtelephone_ed);
        final EditText password_ed = (EditText)findViewById(R.id.logpassword_ed);

        boolean checkbox = sp.getBoolean("checkbox",false);

        if (checkbox){
            username_ed.setText(sp.getString("username",""));
            password_ed.setText(sp.getString("password",""));
            remember_cb.setChecked(true);
        }


        register_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });

        login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences.Editor editor = sp.edit();
                username = username_ed.getText().toString();
                password = password_ed.getText().toString();

                Retrofit retrofit = new Retrofit
                        .Builder()
                        .baseUrl(Const.BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                RetrofitServices service = retrofit.create(RetrofitServices.class);
                Call<Login> call = service.login(username, password);
                call.enqueue(new Callback<Login>() {
                    @Override
                    public void onResponse(Call<Login> arg0, Response<Login> arg1) {
                        if (arg1.isSuccessful()) {
                            Login result = arg1.body();
                            if(result.getTel().toString().equals("0")){
                                Toast.makeText(LoginActivity.this,
                                        "用户名或密码错误",
                                        Toast.LENGTH_SHORT).show();
                            }else {
                                Toast.makeText(LoginActivity.this,
                                        "登录成功",
                                        Toast.LENGTH_SHORT).show();
                                MainActivity mainActivity= (MainActivity) getBaseContext();
                                if (mainActivity!=null){
//                                    mainActivity.replaceLoginFragment();
//                                    mainActivity.setSavedUserId(result.getUserid());
                                    setUserId(result.getId());
                                }
                            }
                        }

                    }
                    @Override
                    public void onFailure(Call<Login> call, Throwable t) {

                    }

                });


                    editor.putString("username",username);
                editor.putString("password",password);

                if (remember_cb.isChecked()){
                    editor.putBoolean("checkbox",true);
                }
                else {
                    editor.putBoolean("checkbox",false);
                }

                editor.commit();
            }
        });



    }
}
